package com.zjsru.product.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.zjsru.product.pojo.Product;
import com.zjsru.product.pojo.Result;
import com.zjsru.product.service.ProductService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
public class ProductController {


    @Resource
    private ProductService productService;

    /**
     * 创建商品
     * @param product
     * @return
     */
    @PostMapping("/product/add")
    public Result createCategory(@RequestBody Product product){
        try {
            productService.createCategory(product);
            return Result.success("创建商品成功");
        }catch (Exception e){
            return Result.error(500, e.getMessage());
        }
    }

    /**
     * 删除商品
     * @param productId
     * @return
     */
    @DeleteMapping("/product/delete")
    public Result deleteCategory(@RequestParam("productId") Integer productId){
        try {
            productService.deleteCategory(productId);
            return Result.success("删除商品成功");
        }catch (Exception e){
            return Result.error(500, e.getMessage());
        }
    }

    /**
     * 修改商品
     * @param product
     * @return
     */
    @PutMapping("/product/update")
    public Result updateCategory(@RequestBody Product product){
        try {
            productService.updateCategory(product);
            return Result.success("商品修改成功");
        }catch (Exception e){
            return Result.error(500, e.getMessage());
        }
    }


    /**
     * 查询商品
     * @param productName
     * @return
     */
    @GetMapping("/product/search")
    public Result searchCategory(@RequestParam(required = false,name = "productName") String productName){
        try {
            List<Product> products = productService.searchCategory(productName);
            if (products.isEmpty()){
                return Result.success("没有该商品");
            }
            return Result.success(products);
        }catch (Exception e)
        {
            return Result.error(500,e.getMessage());
        }
    }


    @GetMapping("/product/query")
    public Result queryProductById(@RequestParam("productId") Long productId){
        try {
            Product product = productService.queryProductById(productId);
            return Result.success(product);
        }catch(Exception e){
            return Result.error(500,e.getMessage());
        }
    }
    /**
     *增加或者减少库存
     * @param integer
     * @param productId
     * @return
     */
    @PutMapping("/product/updateCount")
    @SentinelResource(
            value = "updateProductCount",
            blockHandler = "blockHandlerForUpdateProductCount",
            blockHandlerClass = { SentinelBlockHandler.class }
    )
    public Result updateProductCount(@RequestParam("integer") Integer integer, @RequestParam("productId") Long productId) {
        try {
            productService.updateProductCount(integer, productId);
            return Result.success("库存更新成功");
        } catch (RuntimeException e) {
            return Result.error(500, "库存更新失败: " + e.getMessage());
        }
    }

    public static class SentinelBlockHandler {
        //
        public static Result handleUpdateCountBlock(Integer integer, Long productId, BlockException ex) {
            // 监控告警
            String msg = String.format("库存接口被限流: 操作=%d, 产品ID=%d", integer, productId);
            log.warn(msg);

            // 返回限流提示
            return Result.error(429, "请求过于频繁，请稍后再试");
        }
    }


}
